<%
	{
		/*<!-- Initiation -->*/
		response.setContentType("text/html; charset=UTF-8");
		canss.web.controls.WebDBControl webdbcontrol = null;
		webdbcontrol = (webdbcontrol == null) ? new canss.web.controls.WebDBControlBean() : webdbcontrol;

		/*<!-- Preparing inputs -->*/
		int i = 0;

		/*<!-- Query Compilation -->*/
		canss.supports.Makequery_select query = new canss.supports.Makequery_select().Select("date(canssevent_timecreatedatt_timestamp), group_concat(canssevent_extradata)").PushFrom("tblEvent").PushWhere("tblMID_mid='130'").PushWhere("tblEventDescription_eventid='401'").GroupBy("date(canssevent_timecreatedatt_timestamp)").OrderBy("date(canssevent_timecreatedatt_timestamp) DESC");

		java.util.Iterator<String[]> iter = null;
		java.util.Hashtable<String, Integer> hash = null;
		try
		{
			java.util.LinkedList<String[]> res = webdbcontrol.Select(query);
			iter = res.iterator();
			if (iter.hasNext())
			{
				String[] curhashtmp = iter.next();
				hash = new java.util.Hashtable<String, Integer>(curhashtmp.length);
				for (i = 0; i < curhashtmp.length; i++)
					hash.put(curhashtmp[i], i);
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		java.util.Hashtable<String, Integer> hashcids = new java.util.Hashtable<String, Integer>();
		{
			String _pid = canss.supports.basic.IIf((String)request.getParameter("_pid"), "");
			canss.supports.Makequery_select q = new canss.supports.Makequery_select().Select("tblCID_cid,tblPID_pid").PushFrom("tblContentDescription").PushWhere("tblPID_pid='"+_pid+"'");
			java.util.Iterator<String[]> iter2 = null;
			java.util.Hashtable<String, Integer> hash2 = null;
			try
			{
				java.util.LinkedList<String[]> res = webdbcontrol.Select(q);
				iter2 = res.iterator();
				if (iter2.hasNext())
				{
					String[] curhashtmp = iter2.next();
					hash2 = new java.util.Hashtable<String, Integer>(curhashtmp.length);
					for (i = 0; i < curhashtmp.length; i++)
						hash2.put(curhashtmp[i], i);
				}
			} catch (Exception e)
			{
				e.printStackTrace();
			}
			while (iter2 != null && iter2.hasNext())
			{
				String[] cur = iter2.next();
				String tmp = cur[hash2.get("tblCID_cid")];
				if( !hashcids.containsKey(tmp) )
					hashcids.put(tmp, 1);
			}
		}

		/*<!-- Parsing preparation -->*/
		String pattern0 = "\\s*(<\\/log>)?<log>\\s*";
		String pattern1 = ".*<cs-media-name>\\s?(.+)\\.\\w*\\s?<\\/cs-media-name>.*";
		String pattern2 = ".*<sc-bytes>\\s?(.+)\\s?<\\/sc-bytes>.*";

		java.util.regex.Pattern p0 = java.util.regex.Pattern.compile(pattern0);
		java.util.regex.Pattern p1 = java.util.regex.Pattern.compile(pattern1);
		java.util.regex.Pattern p2 = java.util.regex.Pattern.compile(pattern2);

		/*<!-- Data processing -->*/
		//hash struct
		final class struct_daybyday
		{
			public java.util.Hashtable<String, Integer> played_count_per_content;
			public java.util.Hashtable<String, Integer> traffic_per_content;
		}
		//hashtable
		java.util.Hashtable<String, struct_daybyday> hash_daybyday = new java.util.Hashtable<String, struct_daybyday>();
		//total played count
		int total_played_count = 0;
		//played count per content, traffic_per_content
		struct_daybyday tmp_struct_daybyday = null;

		//processing
		java.util.regex.Matcher m = null;
		while (iter != null && iter.hasNext())
		{
			String[] cur = iter.next();
			String date = cur[hash.get("date(canssevent_timecreatedatt_timestamp)")];
			String tmp = cur[hash.get("group_concat(canssevent_extradata)")];
			
			//prepare tmp structure
			if (hash_daybyday.containsKey(date))
				tmp_struct_daybyday = hash_daybyday.remove(date);
			else
			{
				tmp_struct_daybyday = new struct_daybyday();
				tmp_struct_daybyday.played_count_per_content = new java.util.Hashtable<String, Integer>();
				tmp_struct_daybyday.traffic_per_content = new java.util.Hashtable<String, Integer>();
			}

			{//fill up the structure
				String[] m0matches = p0.split(tmp);
				for (String m0match : m0matches)
				{
					if (canss.supports.basic.isEmpty(m0match))
						continue;
					String innertmp = m0match;
					//
					//check user
					m = p1.matcher(innertmp);
					if(m.find())
					{
						String a = m.replaceAll("$1");
						if( !hashcids.containsKey(a) )
							continue;
					}
					//
					m = p1.matcher(innertmp);
					if (m.find())
					{
						String a = m.replaceAll("$1");
						Integer b = null;
						if (tmp_struct_daybyday.played_count_per_content.containsKey(a))
							b = tmp_struct_daybyday.played_count_per_content.remove(a);
						else
							b = new Integer(0);
						b++;
						tmp_struct_daybyday.played_count_per_content.put(a, b);
						//
						m = p2.matcher(innertmp);
						if (m.find())
						{
							Integer bb = null;
							if (tmp_struct_daybyday.traffic_per_content.containsKey(a))
								bb = tmp_struct_daybyday.traffic_per_content.remove(a);
							else
								bb = new Integer(0);
							
							try
							{
								String aa = m.replaceAll("$1");
								bb += Integer.parseInt(aa);
							}catch(NumberFormatException e)
							{
								bb += 0;
							}
							tmp_struct_daybyday.traffic_per_content.put(a, bb);
						}
					}
				}
			}
			//apply tmp structre
			hash_daybyday.put(date, tmp_struct_daybyday);
			total_played_count++;
		}

		/*<!-- Data representation -->*///format: [ [['',''],['',12]...], ~~ ]
		out.write("[");
		//total played count
		out.write(String.format("[['totalplayedcount',%d]]", total_played_count));
		out.write(",");
		//played count per content
		{
			out.write("[");
			java.util.Enumeration<struct_daybyday> iter_hash_daybyday = hash_daybyday.elements();
			while (iter_hash_daybyday.hasMoreElements())
			{
				tmp_struct_daybyday = iter_hash_daybyday.nextElement();
				java.util.Iterator<String> iter_keys = tmp_struct_daybyday.played_count_per_content.keySet().iterator();
				while (iter_keys.hasNext())
				{
					String keytmp = iter_keys.next();
					out.write(String.format("['%s',[", keytmp));
					out.write(String.format("['played_count_per_content',%s]", canss.supports.basic.IIf("" + tmp_struct_daybyday.played_count_per_content.get(keytmp), "0")));
					out.write(",");
					out.write(String.format("['traffic_per_content',%s]", canss.supports.basic.IIf("" + tmp_struct_daybyday.traffic_per_content.get(keytmp), "0")));
					out.write("]]");
					if (iter_keys.hasNext())
						out.write(",");
				}
				if (iter_hash_daybyday.hasMoreElements())
					out.write(",");
			}
			out.write("]");
		}
		out.write("]");
	}
%>